﻿#define _CRT_SECURE_NO_WARNINGS 1

#include "Contact.h"
#include "SeqList.h"

//通讯录初始化和销毁
void ContactInit(Con* pcon)
{
	SLInit(pcon);
}

void ContactDestroy(Con* pcon)
{
	SLDestroy(pcon);
}

//增加联系人
void ContactAdd(Con* pcon) {
	Info info;

	printf("请输入联系人姓名：\n");
	scanf("%s", info.name);
	printf("请输入联系人年龄：\n");
	scanf("%d", &info.age);
	printf("请输入联系人性别：\n");
	scanf("%s", info.gender);
	printf("请输入联系人电话：\n");
	scanf("%s", info.tel);
	printf("请输入联系人住址：\n");
	scanf("%s", info.addr);

	SLPushBack(pcon, info);

	printf("联系人添加成功\n");
}

int FindByName(Con* pcon,char name[])
{
	for (int i = 0; i < pcon->size; i++)
	{
		if (strcmp(pcon->arr[i].name, name) == 0)
		{
			return i;
		}
	}
	return -1;
}
//删除联系人
void ContactDel(Con* pcon)
{
	char name[NAME_MAX];
	printf("请输入要删除的联系人:>>");
	scanf("%s", &name);
	int ret = FindByName(pcon, name);
	if (ret < 0)
	{
		printf("联系人不存在!\n");
		return;
	}
	SLErase(pcon, ret);
	printf("联系人删除成功!\n");
}
void ContactModify(Con* pcon)
{
	char name[NAME_MAX];
	printf("请输入要修改的联系人:>>");
	scanf("%s", &name);
	int ret = FindByName(pcon, name);
	if (ret < 0)
	{
		printf("联系人不存在!\n");
		return;
	}
	printf("请输入姓名：\n");
	scanf("%s", pcon->arr[ret].name);
	printf("请输入年龄：\n");
	scanf("%d", &pcon->arr[ret].age);
	printf("请输入性别：\n");
	scanf("%s", pcon->arr[ret].gender);
	printf("请输入电话：\n");
	scanf("%s", pcon->arr[ret].tel);
	printf("请输入地址：\n");
	scanf("%s", pcon->arr[ret].addr);

	printf("联系人修改成功！\n");
}
void ContactFind(Con* pcon)
{
	char name[NAME_MAX];
	printf("请输入要修改的联系人:>>");
	scanf("%s", &name);
	int ret = FindByName(pcon, name);
	if (ret < 0)
	{
		printf("联系人不存在!\n");
		return;
	}
	printf("%s %s %s %s %s\n", "姓名", "年龄", "性别", "电话", "住址");
	printf("%s %d %s %s %s\n",
		pcon->arr[ret].name,
		pcon->arr[ret].age,
		pcon->arr[ret].gender,
		pcon->arr[ret].tel,
		pcon->arr[ret].addr
	);
}

void ContactShow(Con* pcon)
{
	printf("%s %s %s %s %s\n", "姓名", "年龄", "性别", "电话", "住址");
	for (int i = 0; i < pcon->size; i++)
	{
		printf("%s %d %s %s %s",
			pcon->arr[i].name, 
			pcon->arr[i].age,
			pcon->arr[i].gender,
			pcon->arr[i].tel,
			pcon->arr[i].addr
		);
		printf("\n");
	}

}
